class CountDownView extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.getViews, this);
    }
    //每周二，日开启

    private timecountdown: BaseTextField;
    private getViews() {
        let countdown = utils.createBitmapByName('count_down');
        countdown.width = 274 * 0.88;
        countdown.height = countdown.width / 274 * 83;
        this.addChild(countdown);
        this.timecountdown = new BaseTextField;
        this.timecountdown.textColor = 0xffea22;
        this.timecountdown.stroke = 2;
        this.timecountdown.strokeColor = 0x5a3b0f;
        this.timecountdown.size = 24;
        this.timecountdown.x = 100;
        this.timecountdown.y = 40;
        this.addChild(this.timecountdown);
        this.timenotice = new BaseTextField;
        this.timenotice.text = '开始倒计时';
        this.timenotice.textColor = 0xefd78c;
        this.timenotice.stroke = 2;
        this.timenotice.strokeColor = 0x492a05;
        this.timenotice.x = 75;
        this.timenotice.y = 6;
        this.timenotice.size = 20;
        this.addChild(this.timenotice);
    }


    private timenotice: BaseTextField;
    private countdown: egret.Timer;
    private countdown2:egret.Timer;
    public refreshTime(time: number, beginning: boolean = false) {
        this.timecountdown.text = utils.timerSecondFormat(time);
        if (beginning) {
            this.timenotice.text = '结束倒计时';
            if(this.countdown) this.countdown.stop(); 
            var countdown = new egret.Timer(1000);
            countdown.addEventListener(egret.TimerEvent.TIMER, () => {
                GameData.lefTime =  time--;
                if (time == 0) {
                    countdown.stop();
                    ViewManager.initView.gameOver();
                    ViewManager.initView.removeChild(ViewManager.initView.countdownView);
                    GameData.hasBeginning = false;
                }
                this.timecountdown.text = utils.timerSecondFormat(time);
            }, this);
            countdown.start();
            this.countdown = countdown;
        } else {
            this.timenotice.text = '开始倒计时';
            if(this.countdown2) this.countdown2.stop();
            var countdown2 = new egret.Timer(1000);
            countdown2.addEventListener(egret.TimerEvent.TIMER, () => {
                time--;
                if (time == 0) {
                    countdown2.stop();
                    ViewManager.initView.getHome();
                }
                this.timecountdown.text = utils.timerSecondFormat(time);
            }, this);
            countdown2.start();
            this.countdown2.stop();
        }

    }
}